package com.ht.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.ht.entity.Result;
import com.ht.modules.system.entity.DbBackFile;
import com.ht.modules.system.service.IDbBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *  数据库备份
 * @Author:
 * @Date: 2021-11-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "数据库备份")
@RestController
@RequestMapping("/dbBack/dbBack")
public class DbBackController {

    @Autowired
    private IDbBackService dbBackService;

    /**
     * 获取数据库备份文件列表
     */
    @ApiOperation(value = "数据管理-获取数据库备份文件列表", notes = "数据管理-获取数据库备份文件列表")
    @GetMapping(value = "/dbBackFileList")
    public Result<?> dbBackFileList() {
        List<DbBackFile> list = dbBackService.dbBackFileList();
        return Result.ok(list);
    }

    /**
     * 生成备份文件
     */
    @ApiOperation(value = "数据管理-生成备份文件", notes = "数据管理-生成备份文件")
    @GetMapping(value = "/createDbBack")
    public Result<?> createDbBack() {
        try {
            dbBackService.createDbBack();
            return Result.ok("生成成功！");
        } catch (Exception e) {
            return Result.error("生成失败：" + e.getMessage());
        }
    }

    /**
     * 备份文件下载
     */
    @ApiOperation(value = "数据管理-备份文件下载", notes = "数据管理-备份文件下载")
    @GetMapping(value = "/downDbBackFile")
    public void downDbBackFile(@RequestParam(name = "fileName") String fileName, HttpServletResponse response) {
        dbBackService.downDbBackFile(fileName, response);
    }

    /**
     * 删除备份文件
     */
    @ApiOperation(value = "数据管理-删除备份文件", notes = "数据管理-删除备份文件")
    @DeleteMapping(value = "/deleteDbBackFile")
    public Result<?> deleteDbBackFile(@RequestParam(name = "fileName") String fileName) {
        try {
            dbBackService.deleteDbBackFile(fileName);
            return Result.ok("删除成功！");
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }

}
